# coding=utf-8
import time
import os
from selenium import webdriver

class BaiduSearch(object):
    dir = os.path.dirname(os.path.abspath('.'))
    chrome_driver_path = dir + '/tools/chromedriver.exe'
    driver = webdriver.Chrome(chrome_driver_path)
    #driver.maximize_window()
    driver.implicitly_wait(6)

    def open_baidu(self):
        self.driver.get("https://www.baidu.com")
        time.sleep(1)

    def test_search(self):
        self.driver.find_element_by_id('kw').send_keys('selenium')
        time.sleep(1)
        print(self.driver.title)
        try:
            assert 'selenium' in self.driver.title
            print('test pass')
        except Exception as e:
            print('test fail')

    def split_str(self):
        search_result_string = self.driver.find_element_by_xpath("//*/div[@class='nums']").text
        print("text",search_result_string)
        new_string = search_result_string.split(u'约')[1]
        print(new_string)
        last_result = new_string.split(u'个')[0]
        print(last_result)
if __name__ =='__main__':
    baidu = BaiduSearch()
    baidu.open_baidu()
    baidu.test_search()
    baidu.split_str()
